//
//  NSDate+Extend.m
//  Test
//
//  Created by lizq on 15/11/20.
//  Copyright © 2015年 lizq. All rights reserved.
//

#import "NSDate+Extend.h"

@implementation NSDate (Extend)

- (NSInteger)year {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    return [calendar component:NSCalendarUnitYear fromDate:self];
}

- (NSInteger)month {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    return [calendar component:NSCalendarUnitMonth fromDate:self];
}

- (NSInteger)weekDay {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    return [calendar component:NSCalendarUnitWeekday fromDate:self];
}

- (NSInteger)day {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    return [calendar component:NSCalendarUnitDay fromDate:self];
}

- (NSInteger)hour {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    return [calendar component:NSCalendarUnitHour fromDate:self];
}

- (NSInteger)minute {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    return [calendar component:NSCalendarUnitMinute fromDate:self];
}

- (NSInteger)second {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    return [calendar component:NSCalendarUnitSecond fromDate:self];
}

- (BOOL)isTady {
    return [self isSameDay:[NSDate date]];
}

- (BOOL)isSameDay:(NSDate *)date {
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd"];
    NSString *selfDate = [formater stringFromDate:self];
    NSString *tmpDate = [formater stringFromDate:date];
    return [selfDate isEqualToString:tmpDate];
}

- (NSString *)dateWithFormater:(NSString *)formater {
    NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
    NSString *result = [dateFormater stringFromDate:self];
    return result;
}

/*
 *  timestamp 秒 10位
 */
- (NSString *)secondTimestamp {
    NSString *timestamp = [NSString stringWithFormat:@"%.0f",[self timeIntervalSince1970]];
    return timestamp;
}

/*
 * timestamp 毫秒 13位
 */
- (NSString *)millSecondTimestamp {
    NSString *timestamp = [NSString stringWithFormat:@"%.0f",[self timeIntervalSince1970] * 1000];
    return timestamp;
}

@end
